using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ServerPanel : BasePanel
{
    public Button buttonChange;
    public Button buttonStart;
    public Button buttonBcak;
    public TextMeshProUGUI ServerName;
    public override void Init()
    {
        buttonBcak.onClick.AddListener(() =>
        {
            //避免返回时重新登录（由于自动登录）
            if (LoginManager.Instance.LoginData.isAutoLogin)
            {
                LoginManager.Instance.LoginData.isAutoLogin = false;
            }
            UIManager.Instance.ShowPanel<LoginPanel>();
            UIManager.Instance.HidePanel<ServerPanel>();
        });
        buttonChange.onClick.AddListener(() =>
        {
            //隐藏自己
            UIManager.Instance.HidePanel<ServerPanel>();
            //显示服务器选择面板
            UIManager.Instance.ShowPanel<ChooseServerPanel>();
        });
        buttonStart.onClick.AddListener(() =>
        {
            //进入游戏
            //隐藏自己
            UIManager.Instance.HidePanel<ServerPanel>();
            //隐藏背景图
            UIManager.Instance.HidePanel<LoginBKPanel>();

            //存储进入的服务器信息
            LoginManager.Instance.SaveLoginData();
            //切换到游戏场景
            SceneManager.LoadScene("GameScene");
        });
    }
    public override void ShowMe()
    {
        base.ShowMe();
        //更新显示数据
        int id = LoginManager.Instance.LoginData.serverId;
        if (id > 0)
        {
            ServerInfo serverInfo = LoginManager.Instance.ServerData[id - 1];
            ServerName.text = serverInfo.id + "区 " + serverInfo.name;
        }
        else
        {
            ServerName.text = "无选择";

        }
    }
}
